home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gp_unifs.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  11.5 KB  |  429 lines

  1. /* Copyright (C) 1993, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gp_unifs.c,v 1.3 2000/09/19 19:00:25 lpd Exp $ */
  20. /* "Unix-like" file system platform routines for Ghostscript */
  21. #include "memory_.h"
  22. #include "string_.h"
  23. #include "gx.h"
  24. #include "gp.h"
  25. #include "gpmisc.h"
  26. #include "gsstruct.h"
  27. #include "gsutil.h"        /* for string_match */
  28. #include "stat_.h"
  29. #include "dirent_.h"
  30. #include <sys/param.h>        /* for MAXPATHLEN */
  31.  
  32. /* Some systems (Interactive for example) don't define MAXPATHLEN,
  33.  * so we define it here.  (This probably should be done via a Config-Script.)
  34.  */
  35.  
  36. #ifndef MAXPATHLEN
  37. #  define MAXPATHLEN 1024
  38. #endif
  39.  
  40. /* Library routines not declared in a standard header */
  41. extern char *mktemp(P1(char *));
  42.  
  43. /* ------ File naming and accessing ------ */
  44.  
  45. /* Define the default scratch file name prefix. */
  46. const char gp_scratch_file_name_prefix[] = "gs_";
  47.  
  48. /* Define the name of the null output file. */
  49. const char gp_null_file_name[] = "/dev/null";
  50.  
  51. /* Define the name that designates the current directory. */
  52. const char gp_current_directory_name[] = ".";
  53.  
  54. /* Create and open a scratch file with a given name prefix. */
  55. /* Write the actual file name at fname. */
  56. FILE *
  57. gp_open_scratch_file(const char *prefix, char fname[gp_file_name_sizeof],
  58.              const char *mode)
  59. {                /* The -8 is for XXXXXX plus a possible final / and -. */
  60.     int len = gp_file_name_sizeof - strlen(prefix) - 8;
  61.  
  62.     if (gp_gettmpdir(fname, &len) != 0)
  63.     strcpy(fname, "/tmp/");
  64.     else {
  65.     if (strlen(fname) != 0 && fname[strlen(fname) - 1] != '/')
  66.         strcat(fname, "/");
  67.     }
  68.     strcat(fname, prefix);
  69.     /* Prevent trailing X's in path from being converted by mktemp. */
  70.     if (*fname != 0 && fname[strlen(fname) - 1] == 'X')
  71.     strcat(fname, "-");
  72.     strcat(fname, "XXXXXX");
  73.     mktemp(fname);
  74.     return gp_fopentemp(fname, mode);
  75. }
  76.  
  77. /* Open a file with the given name, as a stream of uninterpreted bytes. */
  78. FILE *
  79. gp_fopen(const char *fname, const char *mode)
  80. {
  81.     return fopen(fname, mode);
  82. }
  83.  
  84. /* Set a file into binary or text mode. */
  85. int
  86. gp_setmode_binary(FILE * pfile, bool mode)
  87. {
  88.     return 0;            /* Noop under Unix */
  89. }
  90.  
  91. /* ------ File enumeration ------ */
  92.  
  93. /* Thanks to Fritz Elfert (Fritz_Elfert@wue.maus.de) for */
  94. /* the original version of the following code, and Richard Mlynarik */
  95. /* (mly@adoc.xerox.com) for an improved version. */
  96.  
  97. typedef struct dirstack_s dirstack;
  98. struct dirstack_s {
  99.     dirstack *next;
  100.     DIR *entry;
  101. };
  102.  
  103. gs_private_st_ptrs1(st_dirstack, dirstack, "dirstack",
  104.             dirstack_enum_ptrs, dirstack_reloc_ptrs, next);
  105.  
  106. struct file_enum_s {
  107.     DIR *dirp;            /* pointer to current open directory   */
  108.     char *pattern;        /* original pattern                    */
  109.     char *work;            /* current path                        */
  110.     int worklen;        /* strlen (work)                       */
  111.     dirstack *dstack;        /* directory stack                     */
  112.     int patlen;
  113.     int pathead;        /* how much of pattern to consider
  114.                  *  when listing files in current directory */
  115.     bool first_time;
  116.     gs_memory_t *memory;
  117. };
  118. gs_private_st_ptrs3(st_file_enum, struct file_enum_s, "file_enum",
  119.       file_enum_enum_ptrs, file_enum_reloc_ptrs, pattern, work, dstack);
  120.  
  121. /* Private procedures */
  122.  
  123. /* Do a wild-card match. */
  124. #ifdef DEBUG
  125. private bool
  126. wmatch(const byte * str, uint len, const byte * pstr, uint plen,
  127.        const string_match_params * psmp)
  128. {
  129.     bool match = string_match(str, len, pstr, plen, psmp);
  130.  
  131.     if (gs_debug_c('e')) {
  132.     dlputs("[e]string_match(\"");
  133.     fwrite(str, 1, len, dstderr);
  134.     dputs("\", \"");
  135.     fwrite(pstr, 1, plen, dstderr);
  136.     dprintf1("\") = %s\n", (match ? "TRUE" : "false"));
  137.     }
  138.     return match;
  139. }
  140. #define string_match wmatch
  141. #endif
  142.  
  143. /* Search a string backward for a character. */
  144. /* (This substitutes for strrchr, which some systems don't provide.) */
  145. private char *
  146. rchr(char *str, char ch, int len)
  147. {
  148.     register char *p = str + len;
  149.  
  150.     while (p > str)
  151.     if (*--p == ch)
  152.         return p;
  153.     return 0;
  154. }
  155.  
  156. /* Pop a directory from the enumeration stack. */
  157. private bool
  158. popdir(file_enum * pfen)
  159. {
  160.     dirstack *d = pfen->dstack;
  161.  
  162.     if (d == 0)
  163.     return false;
  164.     pfen->dirp = d->entry;
  165.     pfen->dstack = d->next;
  166.     gs_free_object(pfen->memory, d, "gp_enumerate_files(popdir)");
  167.     return true;
  168. }
  169.  
  170. /* Initialize an enumeration. */
  171. file_enum *
  172. gp_enumerate_files_init(const char *pat, uint patlen, gs_memory_t * mem)
  173. {
  174.     file_enum *pfen;
  175.     char *p;
  176.     char *work;
  177.  
  178.     /* Reject attempts to enumerate paths longer than the */
  179.     /* system-dependent limit. */
  180.     if (patlen > MAXPATHLEN)
  181.     return 0;
  182.  
  183.     /* Reject attempts to enumerate with a pattern containing zeroes. */
  184.     {
  185.     const char *p1;
  186.  
  187.     for (p1 = pat; p1 < pat + patlen; p1++)
  188.         if (*p1 == 0)
  189.         return 0;
  190.     }
  191.     /* >>> Should crunch strings of repeated "/"'s in pat to a single "/"
  192.      * >>>  to match stupid unix filesystem "conventions" */
  193.  
  194.     pfen = gs_alloc_struct(mem, file_enum, &st_file_enum,
  195.                "gp_enumerate_files");
  196.     if (pfen == 0)
  197.     return 0;
  198.  
  199.     /* pattern and work could be allocated as strings, */
  200.     /* but it's simpler for GC and freeing to allocate them as bytes. */
  201.  
  202.     pfen->pattern =
  203.     (char *)gs_alloc_bytes(mem, patlen + 1,
  204.                    "gp_enumerate_files(pattern)");
  205.     if (pfen->pattern == 0)
  206.     return 0;
  207.     memcpy(pfen->pattern, pat, patlen);
  208.     pfen->pattern[patlen] = 0;
  209.  
  210.     work = (char *)gs_alloc_bytes(mem, MAXPATHLEN + 1,
  211.                   "gp_enumerate_files(work)");
  212.     if (work == 0)
  213.     return 0;
  214.     pfen->work = work;
  215.  
  216.     p = work;
  217.     memcpy(p, pat, patlen);
  218.     p += patlen;
  219.     *p = 0;
  220.  
  221.     /* Remove directory specifications beyond the first wild card. */
  222.     /* Some systems don't have strpbrk, so we code it open. */
  223.     p = pfen->work;
  224.     while (!(*p == '*' || *p == '?' || *p == 0))
  225.     p++;
  226.     while (!(*p == '/' || *p == 0))
  227.     p++;
  228.     if (*p == '/')
  229.     *p = 0;
  230.     /* Substring for first wildcard match */
  231.     pfen->pathead = p - work;
  232.  
  233.     /* Select the next higher directory-level. */
  234.     p = rchr(work, '/', p - work);
  235.     if (!p) {            /* No directory specification */
  236.     work[0] = 0;
  237.     pfen->worklen = 0;
  238.     } else {
  239.     if (p == work) {    /* Root directory -- don't turn "/" into "" */
  240.         p++;
  241.     }
  242.     *p = 0;
  243.     pfen->worklen = p - work;
  244.     }
  245.  
  246.     pfen->memory = mem;
  247.     pfen->dstack = 0;
  248.     pfen->first_time = true;
  249.     pfen->patlen = patlen;
  250.     return pfen;
  251. }
  252.  
  253. /* Enumerate the next file. */
  254. uint
  255. gp_enumerate_files_next(file_enum * pfen, char *ptr, uint maxlen)
  256. {
  257.     const dir_entry *de;
  258.     char *work = pfen->work;
  259.     int worklen = pfen->worklen;
  260.     char *pattern = pfen->pattern;
  261.     int pathead = pfen->pathead;
  262.     int len;
  263.     struct stat stbuf;
  264.  
  265.     if (pfen->first_time) {
  266.     pfen->dirp = ((worklen == 0) ? opendir(".") : opendir(work));
  267.     if_debug1('e', "[e]file_enum:First-Open '%s'\n", work);
  268.     pfen->first_time = false;
  269.     if (pfen->dirp == 0) {    /* first opendir failed */
  270.         gp_enumerate_files_close(pfen);
  271.         return ~(uint) 0;
  272.     }
  273.     }
  274.   top:de = readdir(pfen->dirp);
  275.     if (de == 0) {        /* No more entries in this directory */
  276.     char *p;
  277.  
  278.     if_debug0('e', "[e]file_enum:Closedir\n");
  279.     closedir(pfen->dirp);
  280.     /* Back working directory and matching pattern up one level */
  281.     p = rchr(work, '/', worklen);
  282.     if (p != 0) {
  283.         if (p == work)
  284.         p++;
  285.         *p = 0;
  286.         worklen = p - work;
  287.     } else
  288.         worklen = 0;
  289.     p = rchr(pattern, '/', pathead);
  290.     if (p != 0)
  291.         pathead = p - pattern;
  292.     else
  293.         pathead = 0;
  294.  
  295.     if (popdir(pfen)) {    /* Back up the directory tree. */
  296.         if_debug1('e', "[e]file_enum:Dir popped '%s'\n", work);
  297.         goto top;
  298.     } else {
  299.         if_debug0('e', "[e]file_enum:Dirstack empty\n");
  300.         gp_enumerate_files_close(pfen);
  301.         return ~(uint) 0;
  302.     }
  303.     }
  304.     /* Skip . and .. */
  305.     len = strlen(de->d_name);
  306.     if (len <= 2 && (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")))
  307.     goto top;
  308.     if (len + worklen + 1 > MAXPATHLEN)
  309.     /* Should be an error, I suppose */
  310.     goto top;
  311.     if (worklen == 0) {        /* "Current" directory (evil un*x kludge) */
  312.     memcpy(work, de->d_name, len + 1);
  313.     } else if (worklen == 1 && work[0] == '/') {    /* Root directory */
  314.     memcpy(work + 1, de->d_name, len + 1);
  315.     len = len + 1;
  316.     } else {
  317.     work[worklen] = '/';
  318.     memcpy(work + worklen + 1, de->d_name, len + 1);
  319.     len = worklen + 1 + len;
  320.     }
  321.  
  322.     /* Test for a match at this directory level */
  323.     if (!string_match((byte *) work, len, (byte *) pattern, pathead, NULL))
  324.     goto top;
  325.  
  326.     /* Perhaps descend into subdirectories */
  327.     if (pathead < pfen->patlen) {
  328.     DIR *dp;
  329.  
  330.     if (((stat(work, &stbuf) >= 0)
  331.          ? !stat_is_dir(stbuf)
  332.     /* Couldn't stat it.
  333.      * Well, perhaps it's a directory and
  334.      * we'll be able to list it anyway.
  335.      * If it isn't or we can't, no harm done. */
  336.          : 0))
  337.         goto top;
  338.  
  339.     if (pfen->patlen == pathead + 1) {    /* Listing "foo/?/" -- return this entry */
  340.         /* if it's a directory. */
  341.         if (!stat_is_dir(stbuf)) {    /* Do directoryp test the hard way */
  342.         dp = opendir(work);
  343.         if (!dp)
  344.             goto top;
  345.         closedir(dp);
  346.         }
  347.         work[len++] = '/';
  348.         goto winner;
  349.     }
  350.     /* >>> Should optimise the case in which the next level */
  351.     /* >>> of directory has no wildcards. */
  352.     dp = opendir(work);
  353. #ifdef DEBUG
  354.     {
  355.         char save_end = pattern[pathead];
  356.  
  357.         pattern[pathead] = 0;
  358.         if_debug2('e', "[e]file_enum:fname='%s', p='%s'\n",
  359.               work, pattern);
  360.         pattern[pathead] = save_end;
  361.     }
  362. #endif /* DEBUG */
  363.     if (!dp)
  364.         /* Can't list this one */
  365.         goto top;
  366.     else {            /* Advance to the next directory-delimiter */
  367.         /* in pattern */
  368.         char *p;
  369.         dirstack *d;
  370.  
  371.         for (p = pattern + pathead + 1;; p++) {
  372.         if (*p == 0) {    /* No more subdirectories to match */
  373.             pathead = pfen->patlen;
  374.             break;
  375.         } else if (*p == '/') {
  376.             pathead = p - pattern;
  377.             break;
  378.         }
  379.         }
  380.  
  381.         /* Push a directory onto the enumeration stack. */
  382.         d = gs_alloc_struct(pfen->memory, dirstack,
  383.                 &st_dirstack,
  384.                 "gp_enumerate_files(pushdir)");
  385.         if (d != 0) {
  386.         d->next = pfen->dstack;
  387.         d->entry = pfen->dirp;
  388.         pfen->dstack = d;
  389.         } else
  390.         DO_NOTHING;    /* >>> e_VMerror!!! */
  391.  
  392.         if_debug1('e', "[e]file_enum:Dir pushed '%s'\n",
  393.               work);
  394.         worklen = len;
  395.         pfen->dirp = dp;
  396.         goto top;
  397.     }
  398.     }
  399.   winner:
  400.     /* We have a winner! */
  401.     pfen->worklen = worklen;
  402.     pfen->pathead = pathead;
  403.     memcpy(ptr, work, len);
  404.     return len;
  405. }
  406.  
  407. /* Clean up the file enumeration. */
  408. void
  409. gp_enumerate_files_close(file_enum * pfen)
  410. {
  411.     gs_memory_t *mem = pfen->memory;
  412.  
  413.     if_debug0('e', "[e]file_enum:Cleanup\n");
  414.     while (popdir(pfen))    /* clear directory stack */
  415.     DO_NOTHING;
  416.     gs_free_object(mem, (byte *) pfen->work,
  417.            "gp_enumerate_close(work)");
  418.     gs_free_object(mem, (byte *) pfen->pattern,
  419.            "gp_enumerate_files_close(pattern)");
  420.     gs_free_object(mem, pfen, "gp_enumerate_files_close");
  421. }
  422.  
  423. /* Test-cases:
  424.    (../?*r*?/?*.ps) {==} 100 string filenameforall
  425.    (../?*r*?/?*.ps*) {==} 100 string filenameforall
  426.    (../?*r*?/) {==} 100 string filenameforall
  427.    (/t*?/?*.ps) {==} 100 string filenameforall
  428.  */
  429.